﻿/*
 * Copyright (c) 2025 粤港澳大湾区（广东）国创中心
 *
 * This file is a part of NctiCAEPre.
 *
 * NctiCAEPre is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 */

#include "MeshQuickSelect.h"
#include "ui_MeshQuickSelect.h"

#include "NctiBaseModel.h"
#include "NctiDocument.h"
#include "NctiCommandManager.h"
#include "NctiDocElementPubFun.h"
#include "NctiArgumentObject.h"
#include "NctiDisplayObject.h"
#include "NctiDisplayDocCustomContainer.h"
#include "NctiDisplayModelBody.h"
#include "NctiGeometryFunction.h"
#include "NctiQtScriptExecutor.h"
#include <QWidget>
#include "Common/SelectModeRoller.h"
#include "FeatureWidget/MessageBox.h"

namespace NCTI {

    MeshQuickSelect::MeshQuickSelect(NctiDocument* i_pDocument, NctiQtScriptExecutor* executor, QWidget* parent) 
        : PopupBaseDialog(parent, true)
        , ui(new Ui::MeshQuickSelect)
        , m_document(i_pDocument)
        , m_executor(executor)
        , m_roller(new SelectModeRoller(i_pDocument, SelectModeRoller::SelectMode::All, this))
    {
        setup();
    }

    MeshQuickSelect::~MeshQuickSelect()
    {
        delete ui;
    }

    bool MeshQuickSelect::beforeAccpet()
    {
        if (m_document == nullptr) {
            return false;
        }

        auto angle = ui->lineEdit_angle->text();
        if (angle.isEmpty()) {
            popupMessage(QStringLiteral("角度值不能为空"));
            return false;
        }

        bool ok = false;
        angle.toDouble(&ok);
        if (!ok) {
            popupMessage(QStringLiteral("角度值必须为有效的数字"));
            return false;
        }

        QString sourceName;
        QList<int> sourceCellIDList;
        if (!getSourceName(sourceName)) {
            popupMessage(QStringLiteral("选取实体对象无效"));
            return false;
        }

        return true;
    }

    void MeshQuickSelect::setup()
    {
        auto widget = new QWidget(this);
        ui->setupUi(widget);
        setTitle(QStringLiteral("快速选择"));
        setWidget(widget);

        ui->pushButton_select->setCheckable(true);
        ui->lineEdit_angle->setText("0.5");
        ui->lineEdit_angle->setValidator(new QRegExpValidator(QRegExp("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$")));

        // 设置NctiSelectType的值
        ui->comboBox->addItem(QStringLiteral("节点"), 1);    // ELEM_Vertex = 1
        ui->comboBox->addItem(QStringLiteral("单元面"), 3);  // ELEM_Face = 3
        ui->comboBox->addItem(QStringLiteral("单元线"), 4);  // ELEM_Edge = 4
        ui->comboBox->setCurrentIndex(0);
        
        connect(ui->pushButton_select, &QPushButton::clicked, this, [this](bool checked) {
            if (!checked) {
                ui->pushButton_select->setChecked(true);
            }

            //std::vector<NctiDisplayObject*> selectObjects;
            //std::vector<void*> selectCells;
            //NctiDocElementPubFun::GetDisplayObjectFromSelManager(m_document, selectObjects, selectCells);
            //setObjectSelection(selectObjects, selectCells);
            });

        connect(this, &MeshQuickSelect::sigConfrim, this, [this] {
            if (!beforeAccpet()) {
                return;
            }

            NctiCommandManager* pComMan = NctiCommandManager::GetCommandManager(m_document->GetBaseModel());
            NctiArgumentObject argumentObject;
            argumentObject.SetAngle1(ui->lineEdit_angle->text().toDouble());
            argumentObject.SetIntValue(ui->comboBox->currentData().toInt());
            pComMan->RunCommand("cmd_ncti_mesh_fast_select", &argumentObject);
            accept();
            });
        
        connect(this, &MeshQuickSelect::sigCancle, this, &MeshQuickSelect::reject);
    }
    
    void MeshQuickSelect::setObjectSelection(const std::vector<NctiDisplayObject*>& objectList, const std::vector<void*>& cellIDList)
    {
        m_selectObjects.clear();
        if (objectList.size() != 1) {
            ui->label_count->setText("0");
            ui->pushButton_select->setChecked(false);
            popupMessage(QStringLiteral("选择实体对象数量不为1"));
        }
        else {
            m_selectObjects = objectList;
            ui->label_count->setText(QString::number(m_selectObjects.size()));
            ui->pushButton_select->setChecked(m_selectObjects.size());
        }
    }

    bool MeshQuickSelect::getSourceName(QString& name)
    {
        if (m_selectObjects.empty()) {
            return false;
        }

        name = m_selectObjects[0]->GetObjName();
        return true;
    }
    
    void MeshQuickSelect::popupMessage(const QString& message, const QString& title)
    {
        MessageBox::Instance()->showMessageBox(message, title, false);
        MessageBox::Instance()->hide();
        //MessageBox::Instance()->exec();
    }
}

